package com.util;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import javax.imageio.stream.FileImageOutputStream;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.xml.crypto.Data;

import com.entity.Teacher;

public class CommonUtil {

	public static void setMemu(JMenu menu,String title){
		Font menuFont=new Font(null,Font.BOLD,24);
		menu.setFont(menuFont);
		menu.setForeground(new Color(255,215,0));
		menu.setText(title);
	}
	public static void setMemuItem(JMenuItem menuItem){
		Font menuFont=new Font(null,Font.BOLD,18);
		menuItem.setBackground(Color.LIGHT_GRAY);
		menuItem.setFont(menuFont);
	}
	
	public static void setFont(JComponent component, int fontNUm){
		if(fontNUm==1){
			component.setFont(new Font(null,Font.BOLD,24));
		}else if(fontNUm==2){
			component.setFont(new Font("宋体",Font.BOLD,18));
		}else if(fontNUm==3){
			component.setFont(new Font("Microsoft JhengHei Light", Font.BOLD, 48));
		}else if(fontNUm==4){
			component.setFont(new Font("隶书", Font.BOLD, 48));
		}else if(fontNUm==5){
			component.setFont(new Font("宋体",Font.PLAIN,18));

		}else if(fontNUm==6){
			component.setFont(new Font("宋体",Font.PLAIN,16));
		}else if(fontNUm==7){
			component.setFont(new Font("宋体",Font.BOLD,16));
		}else if(fontNUm==8){
			component.setFont(new Font("宋体",Font.PLAIN,24));
		}else if(fontNUm==9){
			component.setFont(new Font(null,Font.BOLD,20));
		}else if(fontNUm==10){
			component.setFont(new Font("隶书", Font.BOLD, 36));
		}
	}
	
	public static String getRandom(int length){
		StringBuffer sb=new StringBuffer();
		Random random=new Random();
		for(int i=0;i<length;i++){
		sb.append(random.nextInt(9));
		}
		return sb.toString();
	}
	
	public static int getAge(String birthDate) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
		Date birthStr=sdf.parse(birthDate);
		Calendar cal=Calendar.getInstance();
		if(cal.before(birthStr)){
			throw new IllegalArgumentException("The birthday is before Now.It's unbelievable!");
		}
		
		int yearNow=cal.get(Calendar.YEAR);
		int monthNow=cal.get(Calendar.MONTH);
		int dayOfMonthNow=cal.get(Calendar.DAY_OF_MONTH);
		
		cal.setTime(birthStr);
		int yearBirth=cal.get(Calendar.YEAR);
		int monthBirth=cal.get(Calendar.MONTH);
		int dayOfMonthBirth=cal.get(Calendar.DAY_OF_MONTH);
		
		int age = yearNow - yearBirth;
		if(monthNow<=monthBirth){
			if(monthNow==monthBirth){
				if(dayOfMonthNow<dayOfMonthBirth)age--;
			}else{
				age--;
			}
		}
		
		return age;
	}
	
	public static JButton createButton(JComponent component,Color c,String buttonText,int x,int y,int width,int height){
		JButton button = new JButton(buttonText);
        setFont(button, 7);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setBackground(c);
        button.setBounds(x, y, width, height);
        button.setForeground(new Color(64,60,69));
        component.add(button);
        return button;
	}
	
	
	public static JPanel createFinPanel(String initText){
		JPanel findPanel=new JPanel();
		findPanel.setLayout(null);
		findPanel.setSize(300, 35);
		findPanel.setBackground(Color.white);
		findPanel.setBorder(BorderFactory.createLineBorder(new Color(104,118,246)));
		
		final JTextField findField=new JTextField();
		CommonUtil.setFont(findField, 5);
		findField.setText(" "+initText);
		findField.setForeground(Color.gray);
		findField.setBounds(1, 1, 266, 33);
		findField.setBackground(Color.white);
		findField.setBorder(BorderFactory.createEmptyBorder());
		findField.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {
			}
			public void focusGained(FocusEvent e) {
				if(findField.getForeground()==Color.gray){
					findField.setForeground(Color.darkGray);
					findField.setText("");
				}
				
			}
		});
		findPanel.add(findField);
		
		ImageIcon findIcon=new ImageIcon("src/com/util/find.jpg");
		findIcon.setImage(findIcon.getImage().getScaledInstance(30,33,Image.SCALE_DEFAULT));
		JLabel findLabel=new JLabel();
		findLabel.setBounds(268,1,30,32);
		findLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		findLabel.setBackground(new Color(255,255,255));
		findLabel.setIcon(findIcon);
		findPanel.add(findLabel);
		
		
		return findPanel;
	}
	
	public static JLabel creatLabel(JComponent component,String labeText,int x,int y,int width,int height){
		JLabel label=new JLabel(labeText,JLabel.CENTER);
		CommonUtil.setFont(label, 1);
		label.setBounds(x, y, width, height);
		component.add(label);
		return label;
	}
	
	public static JTextField creaTextField(JComponent component,int x,int y,int width,int height){
		final JTextField textField=new JTextField();
		CommonUtil.setFont(textField, 8);
		textField.setBorder(BorderFactory.createEmptyBorder());
		textField.setOpaque(true);
		textField.setBounds(x, y, width, height);
		component.add(textField);
		textField.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				textField.setBackground(new Color(255,255,255));
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				textField.setBackground(new Color(254,254,215));
			}
		});
		
		return textField;
	}
	
	public  static byte[] getPictureByte(File file) {
		byte[] picture=null;
        try {
			FileInputStream in=new FileInputStream(file);
			ByteArrayOutputStream os=new ByteArrayOutputStream();
			byte[] b=new byte[10];
			int n;
			while((n=in.read(b)) !=-1){
				os.write(b, 0, n);
				os.flush();
			}
			in.close();
			os.close();
			picture=os.toByteArray();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	return picture;	
	}
	
	public static File writePictureFile(String id,byte[] picture){
		File file=null;
		if(picture.length<3){
			return null;
		}else{
			FileImageOutputStream imgOutput;
			try {
				String newPath="src/com/util/images/"+id+".jpg";
				file=new File(newPath);
				imgOutput = new FileImageOutputStream(file);
				imgOutput.write(picture, 0, picture.length);
				imgOutput.flush();
				imgOutput.close();	
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return file;
	}
	
}
